
from ...pao_python.pao.data import process_db, dataframe_to_list, DataProcess, DataList
import pandas as pd
import uuid
import datetime
import re
import hashlib
from ...pao_python.pao.service.security.security_service import RoleService
from ...pao_python.pao.service.security.security_utility import get_current_account_id
from ...service.common import insert_data, find_data, update_data, delete_data, get_condition, get_info
from ...service.buss_pub.bill_manage import BillManageService, TypeId, OperationType
from ...service.mongo_bill_service import MongoBillFilter
from server.pao_python.pao.service.data.mongo_db import MongoService, MongoFilter, C, N, F, as_date


class DeviceService(MongoService):
    '''设备管理'''

    def __init__(self, db_addr, db_port, db_name, db_user, db_pwd, inital_password, session):
        self.db_addr = db_addr
        self.db_port = db_port
        self.db_name = db_name
        self.db_user = db_user
        self.db_pwd = db_pwd
        self.inital_password = inital_password
        self.session = session
        self.bill_manage_service = BillManageService(
            db_addr, db_port, db_name, db_user, db_pwd, inital_password, session)

    def get_device(self, org_list, condition, page, count):
        '''
            获取设备列表
            Arguments:
                condition   {dict}      条件
                page        {number}    页码
                count       {number}    条数
        '''
        keys = ['id', 'imei', 'name', 'user_id']
        values = self.get_value(condition, keys)
        _filter = MongoBillFilter()
        _filter.lookup_bill('PT_User', 'user_id', 'id', 'user')\
            .add_fields({
                'user_name': '$user.name', })\
            .match_bill((C('id') == values['id'])
                        & (C('name') == values['name'])
                        & (C('imei') == values['imei'])
                        & (C('user_id') == values['user_id'])
                        & (C('organization_id').inner(org_list)))\
            .project({'_id': 0, 'user._id': 0})
        res = self.page_query(_filter, 'PT_Device', page, count)
        return res

    def update_device(self, device):
        '''绑定设备

        Arguments:
            device   {dict}      条件
        '''
        res = 'fail'

        def process_func(db):
            nonlocal res
            data = get_info({**device}, self.session)
            if 'id' in device.keys():
                bill_id = self.bill_manage_service.add_bill(OperationType.update.value,
                                                            TypeId.device.value, data, 'PT_Device')
                if bill_id:
                    res = 'Success'

        process_db(self.db_addr, self.db_port, self.db_name,
                   process_func, self.db_user, self.db_pwd)
        return res

    def delete_device(self, ids):
        '''解除绑定设备

        Arguments:
            device   {dict}      条件
        '''
        res = 'fail'
        for id in ids:
            data = {'id': id}
            bill_id = self.bill_manage_service.add_bill(OperationType.delete.value,
                                                        TypeId.device.value, data, 'PT_Device')
            if bill_id:
                res = 'Success'

        return res

    def get_device_log(self, org_list, condition, page, count):
        '''
            获取设备日志列表
            Arguments:
                condition   {dict}      条件
                page        {number}    页码
                count       {number}    条数
        '''
        keys = ['id']
        values = self.get_value(condition, keys)
        _filter = MongoBillFilter()
        _filter.match(C('device_id') == values['id'] & (C('organization_id').inner(org_list)))\
            .project({'_id': 0})
        res = self.page_query(_filter, 'PT_Device_Log', page, count)
        return res
